package com.ycz.sas.jgen.ui;


import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ycz.sas.jgen.domain.ServerConfig;
import com.ycz.sas.jgen.handler.FtpOprate;
import com.ycz.sas.jgen.handler.XMLParser;

import net.sf.jftp.net.BasicConnection;

public class FtpConfigPanel extends JPanel{

	private JComboBox cbScheme;
	private JTextField txtName;
	private JTextField txtHost;
	private JTextField txtPort;
	private JTextField txtUser;
	private JPasswordField txtPassword;
	private JTextField txtEASHome;

	private Component owner;

	private BasicConnection con;

	private List<ServerConfig> configList;


	public FtpConfigPanel(Component owner){
		super();

		this.owner = owner;
		this.configList = XMLParser.getServerConfigs();

		initLayout();
	}

	private void initLayout(){
		JLabel contScheme = new JLabel("SCHEME           ");
		contScheme.setSize(100, 10);
		JLabel contName = new JLabel("NAME            ");
		JLabel contHost = new JLabel("HOST            ");
		JLabel contPort = new JLabel("PORT            ");
		JLabel contUser = new JLabel("USER             ");
		JLabel contPassword = new JLabel("PASSWORD");
		JLabel contEASHome = new JLabel("EASHOME    ");

		Vector<String> schemes = new Vector<String>();
		for (int i = 0; i < configList.size(); i++) {
			schemes.add(configList.get(i).getName());
		}
		cbScheme = new JComboBox(schemes);
		txtName = new JTextField(30);
		txtHost = new JTextField(30);
		txtPort = new JTextField(30);
		txtUser = new JTextField(30);
		txtPassword = new JPasswordField(30);
		txtEASHome = new JTextField(30);

		cbScheme.setSelectedIndex(0);
		ServerConfig defaultConfig = configList.get(0);
		updateComponentValue(defaultConfig);


		add(contName);add(txtName);
		add(contHost);add(txtHost);
		add(contPort);add(txtPort);
		add(contUser);add(txtUser);
		add(contPassword);add(txtPassword);
		add(contEASHome);add(txtEASHome);
		add(contScheme);add(cbScheme);

		cbScheme.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent e) {
				int selectedIndex = cbScheme.getSelectedIndex();
				updateComponentValue(configList.get(selectedIndex));
			}

		});
	}

	private void updateComponentValue(ServerConfig config){
		txtName.setText(config.getName());
		txtHost.setText(config.getHost());
		txtPort.setText(config.getPort());
		txtUser.setText(config.getUser());
		txtPassword.setText(config.getPassword());
		txtEASHome.setText(config.getEashome());
	}

	public BasicConnection getConnect(){
		String host = txtHost.getText().trim();
		int port = Integer.valueOf(txtPort.getText().trim());
		String user = txtUser.getText().trim();
		String password = getPassword();
		if(owner instanceof FtpDialogUI){
			FtpDialogUI dialog = (FtpDialogUI) owner;
			return dialog.getFtpOprate().getConnection("ftp", host, port, user, password);
		}
		return new FtpOprate().getConnection("ftp", host, port, user, password);
	}

	public String getEASHome(){
		return txtEASHome.getText().trim();
	}

	public String getPassword(){
		StringBuffer pwdBuffer = new StringBuffer();
		for(char c : txtPassword.getPassword()){
			pwdBuffer.append(c);
		}
		return pwdBuffer.toString();
	}
}
